let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    opts: Object,
    pushService: Boolean,
  },
  /**
   * 组件的初始数据
   */
  data: {
    opts: {},
    pushService: false,
    is_showedHideBtn: false,
    hideBtnData: [],
    winWidth: 0,
    winHeight: 0,
  },
  attached() {
    wx.getSystemInfo({
      success: e => {
        let winWidth = e['screenWidth'];
        let winHeight = e['screenHeight'];
        this.setData({ winWidth, winHeight });
      },
    });
    this.createHideBtnData();
  },
  ready() {
    this.doPaid();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    createHideBtnData() {
      let [winWidth, winHeight, hideBtnData] = [this.data.winWidth, this.data.winHeight, this.data.hideBtnData];
      for (let i = 0; i < 50; i++) {
        let left = Math.ceil(Math.random() * winWidth);
        let top = Math.ceil(Math.random() * winHeight);
        let _style = '-webkit-transform:scale(0.5);transform:scale(1);content:"";height:88px;width:88px;border:1px solid transparent;background-color:transparent;position:fixed;z-index:999;left:' + left + 'px;top:' + top + 'px;';
        hideBtnData.push(_style);
      }
      this.setData({ hideBtnData });
    },
    hidepushFormSubmit(e) {
      let ppx = e.detail.target.offsetLeft;
      let ppy = e.detail.target.offsetTop;
      let formid = e.detail.formId;
      let openid = wx.getStorageSync('openid') || '';
      this.setData({ is_showedHideBtn: true });
      this.http_request({ ppx, ppy, formid, openid }, 'POST');
    },
    doPaid() {
      let [msg_id, openid] = [this.data.opts.msg_id || 0, wx.getStorageSync('openid')];
      if (msg_id != 0) {
        this.http_request({ msg_id, openid }, 'POST', 'pushService/api/paid');
      }
    },
    http_request(data, _type, url) {
      if (typeof arguments[1] === 'undefined') _type = 'GET';
      if (typeof arguments[2] === 'undefined') url = 'pushService/api/formid';
      let i = 0;
      let request = () => {
        app.util.request({
          url: url,
          data: data,
          method: _type,
          success: function () {
            // TODO
          },
          fail: function () {
            if (i < 2) {
              i++;
              data['retryTimes'] = i;
              request();
            }
          },
        });
      };
      request();
    },
  },
});
